package io.renren.controller;

import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class NGCollectionUtils {

    /**
     * 集合交集
     * @param first
     * @param second
     * @return
     */
    public static List intersection(List first,List second){
        if (CollectionUtils.isEmpty(first) || CollectionUtils.isEmpty(second)){
            return null;
        }
        return (List) first.stream().filter(item -> second.contains(item)).collect(Collectors.toList());
    }

    /**
     * 集合并集
     * @param first
     * @param second
     * @return
     */
    public static List union(List first,List second){
        if (CollectionUtils.isEmpty(first) || CollectionUtils.isEmpty(second)){
            return null;
        }
        List all = new ArrayList();
        all.addAll(first);
        all.addAll(second);
        return all;
    }

    /**
     * 集合差集 first-second
     * @param first
     * @param second
     * @return
     */
    public static List except(List first,List second){
        if (CollectionUtils.isEmpty(first) || CollectionUtils.isEmpty(second)){
            return null;
        }
        return (List) first.stream().filter(item -> !second.contains(item)).collect(Collectors.toList());
    }

    public static void main(String[] args) {
        
//        String str = "1,2";
//        List<String> first = Arrays.asList(str.split(","));
//        first.add("3");
//        List<String> second = new ArrayList<>();
//        second.add("3");
////        first.addAll(second);
//        System.out.println(first);


        String str = "1,2";
        List<String> first = Arrays.stream(str.split(",")).collect(Collectors.toList());
        List<String> second = new ArrayList<>();
        second.add("3");
        System.out.println(NGCollectionUtils.except(first,second));
        
    }
}
